/**
 * Created by Administrator on 2017/2/21.
 */
var discount = "";
getDiscount();

/**
 * 提交充值
 */
function submitForm() {
    $("body").trigger("click");

    var qqNum = $("#phoneNumber").val().trim();
    var dimension = $("#hiddenDimension").val();
    var product = $("#hiddenProduct").val();
    var cpType = $("#hiddenCpType").val();

    var qbPrice = $("#sale").text();


    if (!checkQQ(qqNum)) {
        alert(qbPrice);
        // alert("请输入正确QQ号！谢谢");
        return;
    }

    if (isNull(dimension)) {
        alert("请选择充值产品！谢谢");
        return;
    }

    $("#submit_form").attr("disabled", "disabled");
    setTimeout(function() {
        $("#submit_form").removeAttr("disabled");
    }, 3000);

    var url = PAY_PATH + "/6";
    var data = {
        "hiddenAccountNo" : qqNum,
        "hiddenDimension" : dimension,
        "hiddenProduct" : product,
        "cp":cpType,
        "userId":userId,
        "memberId":memberId
    }
    toPay(url,data);
}
/**
 * 获取当前折扣
 */
function getDiscount() {
    var product = "6";
    var cpType = "06";

    $.ajax({
        type : "GET",
        url : OPEN_PATH + "/6/product",
        dataType : 'json', // 返回json格式数据
        data : {
            "pt" : product,
            "cp" : cpType
        }, // 数据
        cache : false,
        success : function(msg) {
            var pro = msg.products;
            if (undefined != pro) {
                discount = pro[0].sale;
            }
        },
        beforeSend : function() {
            $("#loadingToast").show();
        },
        complete : function() {
            $("#loadingToast").hide();
        }
    });
}

/**
 * Q币设置价格
 *
 * @param dimension
 * @param money
 */
function setPrice(obj) {
    var dimension = $(obj).attr("dimension");
    var sale = $(obj).attr("sale");
    // 样式修改
    $("a[name='priceTag']").find("p").css({
        "color" : "#000"
    });
    var selected = $(obj).children("p").children("span");
    if (isNull(discount)) {
        money = sale;
    } else {
        money = ceil4Float(dimension * discount,2);
    }
    selected.text("售价 ¥" + money);
    selected.parent().css({
        "margin-top" : "-10px",
        "color" : "red"
    });
    // 定值
    $("#hiddenDimension").val(dimension);
    var finalPrice = ceil4Float(dimension * discount,2);
    $("#sale").text(finalPrice);
}
/**
 * 输入其他金额
 */
function setOtherPrice() {
    var dimension = $("#otherPrice").val();
    $("a[name='priceTag']").find("p").css({
        "color" : "#000"
    });
    if (dimension == "") {
        $("#sale").text("0.00");
        $("#hiddenDimension").val("");
        return;
    }
    if (!checkQB(dimension)) {
        $("#sale").text("");
        alert("请输入正确面额");
        $("#hiddenDimension").val("");
        return;
    }
    // 定值
    $("#hiddenDimension").val(dimension);
    var sale = ceil4Float(dimension * discount,2);
    $("#sale").text(sale);

}
/**
 * 验证Q币面额
 * @param val
 * @returns {Boolean}
 */
function checkQB(val) {
    return (/^[0-9]*[1-9][0-9]*$/).test(val.toString());
}
/**
 * 验证QQ号
 * @param val
 * @returns
 */
function checkQQ(val) {
    return (/^[1-9][0-9]{4,9}$/).test(val.toString());
}